package LeetCode;

//将号码字母对应关系存储进Map

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javax.swing.UIManager.put;


/*
如果用python代码可以很优美
        ans = ['']
        for num in digits:
            ans = [pre+suf for pre in ans for suf in KEY[num]]
        return ans
*/

public class LC17 {
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<String>();
        List<String> result1 = new ArrayList<String>();
        Map<Character, List<Character>> map = new HashMap<Character, List<Character>>();
        map.put('2', new ArrayList<Character>(){{add('a');add('b');add('c');}});
        map.put('3', new ArrayList<Character>(){{add('d');add('e');add('f');}});
        map.put('4', new ArrayList<Character>(){{add('g');add('h');add('i');}});
        map.put('5', new ArrayList<Character>(){{add('j');add('k');add('l');}});
        map.put('6', new ArrayList<Character>(){{add('m');add('n');add('o');}});
        map.put('7', new ArrayList<Character>(){{add('p');add('q');add('r');add('s');}});
        map.put('8', new ArrayList<Character>(){{add('t');add('u');add('v');}});
        map.put('9', new ArrayList<Character>(){{add('w');add('x');add('y');add('z');}});
        for (char c: digits.toCharArray()) {
            if(result.isEmpty()){
                for (char c1 : map.get(c)) {
                    result1.add(String.valueOf(c1));
                }
            }
            else {
                for (char c1 : map.get(c)) {
                    for (String s : result) {
                        result1.add(s + c1);
                    }
                }
            }
            result.clear();
            result.addAll(result1);
            result1.clear();
        }
        return result;
    }
}
